package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void save(@RequestBody UserFormDTO userFormDTO){
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public UserVO queryById(@PathVariable("id") Long id){
        /*User user = userService.getById(id);
        return BeanUtil.copyProperties(user, UserVO.class);*/
        return userService.queryUserAndAddress(id);
    }

    @ApiOperation("根据id批量查询用户")
    @GetMapping
    public List<UserVO> queryByIds(@RequestParam("ids")List<Long> ids){
        /*List<User> userList = userService.listByIds(ids);
        return BeanUtil.copyToList(userList, UserVO.class);*/
        return userService.queryUserAndAddressByIds(ids);
    }

    @ApiOperation("根据id扣减余额")
    @PutMapping("/{id}/deduction/{amount}")
    public void updateBalance(@PathVariable("id") Long id, @PathVariable("amount") Integer amount){
        userService.deductBalance(id, amount);
    }

    @ApiOperation("根据条件查询用户")
    @PostMapping("/list")
    public List<UserVO> queryUser(@RequestBody UserQuery userQuery){
        /*QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(userQuery.getName()), "username", userQuery.getName())
                .ge(userQuery.getMinBalance() != null, "balance", userQuery.getMinBalance())
                .le(userQuery.getMaxBalance() != null, "balance", userQuery.getMaxBalance())
                .eq(userQuery.getStatus() != null, "status", userQuery.getStatus());
        List<User> userList = userService.list(queryWrapper);*/
        List<User> userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(userQuery.getName()), User::getUsername, userQuery.getName())
                .ge(userQuery.getMinBalance() != null, User::getBalance, userQuery.getMinBalance())
                .le(userQuery.getMaxBalance() != null, User::getBalance, userQuery.getMaxBalance())
                .eq(userQuery.getStatus() != null, User::getStatus, userQuery.getStatus())
                .list();
        return BeanUtil.copyToList(userList, UserVO.class);
    }

}
